package com.hmyuan.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hmyuan.dao.Sys_res_roleMapper;
import com.hmyuan.dao.Sys_res_userMapper;
import com.hmyuan.dao.Sys_roleMapper;
import com.hmyuan.domain.Sys_res_roleKey;
import com.hmyuan.domain.Sys_res_userKey;
import com.hmyuan.domain.Sys_role;
import com.hmyuan.service.SysRoleService;

@Service("sysRoleService")
public class SysRoleServiceImpl implements SysRoleService {

	@Resource
	Sys_roleMapper sys_roleMapper;
	@Resource
	Sys_res_roleMapper sys_res_roleMapper;
	
	@Override
	public PageInfo<Sys_role> selectListByPage(Sys_role sys_role,
			int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, pageSize); // 核心分页代码
		List<Sys_role> list = sys_roleMapper.selectList(sys_role);
		PageInfo<Sys_role> pageInfo = new PageInfo<Sys_role>(list);
		
		return pageInfo;
	}

	@Override
	public boolean insert(Sys_role sys_role) {
		// TODO Auto-generated method stub
		int num = sys_roleMapper.insert(sys_role);
		return num==1?true:false;
	}

	@Override
	public Sys_role selectByPrimaryKey(Long id) {
		// TODO Auto-generated method stub
		return sys_roleMapper.selectByPrimaryKey(id);
	}

	@Override
	public boolean updateByPrimaryKeySelective(Sys_role sys_role) {
		// TODO Auto-generated method stub
		int num = sys_roleMapper.updateByPrimaryKeySelective(sys_role);
		return num==1?true:false;
	}

	@Override
	public List<Sys_role> selectListByUserid(Long userid) {
		// TODO Auto-generated method stub
		return sys_roleMapper.selectListByUserid(userid);
	}

	@Override
	public boolean insertResRoles(Long roleid, Long[] resIds) {
		// TODO Auto-generated method stub
		//添加前清空角色权限
		sys_res_roleMapper.deleteByRoleid(roleid);
		
		if(null==resIds||resIds.length==0) return true;
		
		Sys_res_roleKey sys_res_role = new Sys_res_roleKey();
		sys_res_role.setRoleid(roleid);
		for (long i : resIds) {
			sys_res_role.setResid(i);
			sys_res_roleMapper.insert(sys_res_role);
		}
		return true;
	}

}
